import datetime
from sqlalchemy import desc
from manager.app import db, Base


class Project(Base):
    __tablename__ = 'pt_project'

    project_name = db.Column(db.String(50))

    @classmethod
    def load_project(cls, project_list):
        for project in project_list:
            existed_project = cls.query.filter_by(project_name=project.project_name).first()
            if not existed_project:
                db.session.add(project)
                db.session.commit()

    @classmethod
    def find_project_by_id(cls, project_id):
        return Project.query.filter_by(id=project_id).first()

    def to_dict(self):
        return {
            "project_id": self.id,
            "project_name": self.project_name
        }


class MobileApp(Base):
    __tablename__ = 'pt_mobile_app'

    app_id = db.Column(db.String(50))
    app_name = db.Column(db.String(50))
    app_icon = db.Column(db.String(500))
    package_name = db.Column(db.String(200))
    package_code = db.Column(db.String(200))
    package_sort = db.Column(db.Integer)

    @classmethod
    def load_app(cls, app_list):
        for app in app_list:
            existed_app = cls.query.filter_by(package_code=app.package_code).first()
            if not existed_app:
                db.session.add(existed_app)
                db.session.commit()

    @classmethod
    def load_app_list(cls):
        return cls.query.filter_by().all()


    @classmethod
    def find_app_by_id(cls, app_id):
        return cls.query.filter_by(id=app_id).first()


    def to_dict(self):
        return {
            "app_id": self.id,
            "app_name": self.app_name,
            "app_icon":self.app_icon,
            "package_code": self.package_code,
            "package_name": self.package_name,
            "package_sort": self.package_sort
        }


class AppStore(Base):
    __tablename__ = 'pt_app_store'

    store_id = db.Column(db.String(50))
    store_name = db.Column(db.String(50))
    store_icon = db.Column(db.String(50))
    store_url = db.Column(db.String(500))
    web_site   = db.Column(db.String(500))

    @classmethod
    def load_app_list(cls):
        return cls.query.all()


    @classmethod
    def find_app_by_id(cls, store_id):
        return AppStore.query.filter_by(id=store_id).first()

    def to_dict(self):
        return {
            "store_id": self.id,
            "store_name": self.store_name,
            "store_icon":self.store_icon,
            "store_url": self.store_url,
            "web_site":self.web_site
        }



class AppPage(Base):
    __tablename__='pt_app_page'

    app_id = db.Column(db.String(100))
    page_id = db.Column(db.String(100))
    image_path = db.Column(db.String(500))
    page_ahead = db.Column(db.String(100))


    @classmethod
    def load_page_list(cls, app_id):
        return AppPage.query.filter_by(app_id=app_id).all()




class RobotInstance(Base):
    __tablename__ = 'pt_robot'

    robot_name = db.Column(db.String(100))
    app_id = db.Column(db.INTEGER, nullable=False, index=True)
    command = db.Column(db.String(200))
    params = db.Column(db.String(500))
    result = db.Column(db.String(500))

    @classmethod
    def update_robot_instances(cls, app_id, robot_instance_list):
        for robot_instance in robot_instance_list:
            existed_robot_instance = cls.query.filter_by(app_id=app_id,
                                                          robot_name=robot_instance.robot_name).first()
            if not existed_robot_instance:
                db.session.add(robot_instance)
                db.session.commit()

        for robot in cls.query.filter_by(app_id=app_id).all():
            existed_robot = any(
                robot.robot_name == s.robot_name
                for s in robot_instance_list
            )
            if not existed_robot:
                db.session.delete(robot)
                db.session.commit()

    @classmethod
    def list_robot_by_app_id(cls, app_id):
        return cls.query.filter_by(app_id=app_id).all()

    def to_dict(self):
        return dict(robot_instance_id=self.id,
                    robot_name=self.robot_name,
                    app_id=self.app_id)
